package com.wedo.mall.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.e.library.activity.EActivity;
import com.e.library.listener.EListener;
import com.e.library.utils.EViewUtils;
import com.e.library.widget.ETitleBar;
import com.wedo.mall.R;
import com.wedo.mall.adapter.SellerCategoryAdapter;
import com.wedo.mall.entity.SellerCategory;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lxy on 2017/6/15.
 */

public class SellerCategoryActivity extends EActivity {

    @BindView(R.id.title_bar) ETitleBar title_bar;
    @BindView(R.id.rv_category) RecyclerView rv_category;

    @Override
    protected int layout() {
        return R.layout.activity_seller_category;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        rv_category.setHasFixedSize(true);
        EViewUtils.vertical(rv_category);

        final List<String> selected = new ArrayList<>(10);
        final List<SellerCategory> categories = new ArrayList<>(10);

        for (int i = 0; i < 10; i++) {
            String name = "分类 "+i;
            categories.add(SellerCategory.with(name));
        }

        final SellerCategoryAdapter adapter = new SellerCategoryAdapter(this,categories);
        rv_category.setAdapter(adapter);
        adapter.setItemClickListener(new EListener<SellerCategory>() {
            @Override
            public void onInvoked(View view, SellerCategory data, int position) {
                boolean checked = data.checked;
                if (checked){
                    selected.remove(data.name);
                }else {
                    selected.add(data.name);
                }

                data.checked = !checked;
                adapter.notifyItemChanged(position);
            }
        });

        title_bar.setOnForwardListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selected.isEmpty()){
                    toast("请选择分类");
                    return;
                }

                if (selected.size() >3){
                    toast("最多选三个分类");
                    return;
                }

                StringBuilder sb = new StringBuilder();
                for (int i = 0;i < selected.size();i ++){
                    sb.append(i== 0?"":"、");
                    sb.append(selected.get(i));
                }

                Intent intent = new Intent();
                intent.putExtra("category",sb.toString());
                setResult(RESULT_OK,intent);
                defaultFinish();

            }
        });

    }





}
